//
//  HomeDetailViewController.m
//  coba-iOS
//
//  Created by 郭四虎 on 15/12/29.
//  Copyright © 2015年 YouKu. All rights reserved.
//

#import "HomeDetailViewController.h"

@interface HomeDetailViewController ()<UIWebViewDelegate>
@property (strong, nonatomic) IBOutlet UIVisualEffectView *effectvi;
@property (weak, nonatomic) IBOutlet UIWebView *web;
@property (weak, nonatomic) IBOutlet UIButton *backBtn;

@end

@implementation HomeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    
    _web.scrollView.bounces = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
//    _web.backgroundColor = [UIColor clearColor];
//    [_web setOpaque:NO];
    
    [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlstr]]];
    [self.web Loading_0000];
    
    if (![SHOWMSG boolValue]) {
        
        [self setBtn];
        
        UIAlertView *ale = [[UIAlertView alloc]initWithTitle:@"该服务由携程旅行网提供，详情请咨询携程旅行客服中心" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
        [ale show];
        
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"showmsg"];
    }
    //设置按钮位置
    CGRect rect = self.backBtn.frame;
    //    if (SCALEY>1) {
    //        rect.origin.x -= rect.size.width*(SCALEY-1);
    //    }
    
    rect.size.height = rect.size.height*SCALEY;
    rect.size.width = rect.size.width*SCALEY;
    self.backBtn.frame = rect;
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
    [_backBtn addGestureRecognizer:pan];
}
- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setBtn
{
    //毛玻璃
    UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    _effectvi = [[UIVisualEffectView alloc]initWithEffect:effect];
    _effectvi.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
    
    [self.view addSubview:_effectvi];
    
    _backBtn.enabled = NO;
    
    
    
    [self.view insertSubview:_effectvi belowSubview:_backBtn];
      
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, self.backBtn.y + self.backBtn.height +30, 250, 160);
//    [btn setTitle:@"知道了" forState:UIControlStateNormal];
//    [btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(disMiss) forControlEvents:UIControlEventTouchUpInside];
    [_effectvi addSubview:btn];
    
    UIImageView *ima1 = [[UIImageView  alloc]initWithFrame:CGRectMake(0, 0, 170, 160)];
    ima1.image = [UIImage imageNamed:@"图层-1.png"];
    [btn addSubview:ima1];
    
    UIImageView *ima2 = [[UIImageView alloc]initWithFrame:CGRectMake(180, 0, 70, 65)];
    ima2.image = [UIImage imageNamed:@"知道喽.png"];
    [btn addSubview:ima2];
}
- (void)disMiss
{
    [_effectvi removeFromSuperview];
    _backBtn.enabled = YES;
}

//- (void)setBtn
//{
//    //毛玻璃
//    UIVisualEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
//    _effectvi = [[UIVisualEffectView alloc]initWithEffect:effect];
//    _effectvi.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
//    
//   // [_web addSubview:_effectvi];
//    
////    _backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
////    _backBtn.frame = CGRectMake(WIDTH-49*SCALEY, 100, 49*SCALEY, 49*SCALEY);
//    _backBtn.enabled = NO;
//    [_backBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
//    
//    CGRect rect = self.backBtn.frame;
//    if (SCALEY>1) {
//        rect.origin.x -= rect.size.width*(SCALEY-1);
//    }
//    
//    rect.size.height = rect.size.height*SCALEY;
//    rect.size.width = rect.size.width*SCALEY;
//    self.backBtn.frame = rect;
//    
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
//    [_backBtn addGestureRecognizer:pan];
//    
//}

- (void)handlePan:(UIPanGestureRecognizer *)pan
{
    CGPoint origin = pan.view.frame.origin;
    CGSize size = pan.view.frame.size;
    
    CGPoint translation = [pan translationInView:self.view];
    pan.view.center = CGPointMake(pan.view.center.x + translation.x,
                                         pan.view.center.y + translation.y);
    
    [pan setTranslation:CGPointZero inView:self.view];
    if (pan.state == UIGestureRecognizerStateEnded) {
        
        if (origin.y < 50) {
            
        
        if ((origin.x+size.width/2)<=WIDTH/2){
        
            [UIView animateWithDuration:0.2 animations:^{
                
                pan.view.frame = CGRectMake(0, 50, size.width, size.height);
            }];
        }else{
            [UIView animateWithDuration:0.2 animations:^{
                
                pan.view.frame = CGRectMake(WIDTH - size.width, 50, size.width, size.height);
            }];
        }
        }else if(origin.y > HEIGHT-100){
            
            if ((origin.x+size.width/2)<=WIDTH/2){
                
                [UIView animateWithDuration:0.2 animations:^{
                    
                    pan.view.frame = CGRectMake(0, HEIGHT-100, size.width, size.height);
                }];
            }else{
                [UIView animateWithDuration:0.2 animations:^{
                    
                    pan.view.frame = CGRectMake(WIDTH - size.width, HEIGHT-100, size.width, size.height);
                }];
            }

            
        }else{
            
            if ((origin.x+size.width/2)<=WIDTH/2){
                
                [UIView animateWithDuration:0.2 animations:^{
                    
                    pan.view.frame = CGRectMake(0, origin.y, size.width, size.height);
                }];
            }else{
                [UIView animateWithDuration:0.2 animations:^{
                    
                    pan.view.frame = CGRectMake(WIDTH - size.width, origin.y, size.width, size.height);
                }];
            }
        }
        
    }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *str = [[request URL]absoluteString];
    NSLog(@"%@",str);
    
    
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.web Hidden];
    
//    CGFloat webViewHeight=[_web.scrollView contentSize].height;
//    
//    
//    
//    CGRect newframe = _web.frame;
//    
//    
//    
//    newframe.size.height = webViewHeight;
//    
//    _web.frame = newframe;
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
